package com.example.blogsystem.impl;

import com.example.blogsystem.mapper.UserMapper;
import com.example.blogsystem.model.User;
import com.example.blogsystem.service.UserService;
import com.example.blogsystem.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User getUserById(Integer id){
        return userMapper.queryUserById(id);
    }

    public User getUserByName(String name){
        return userMapper.queryUserByName(name);
    }

    public void insertUser(User user){
        userMapper.insertUser(user);
    }

    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    public User getUserByEmail(String email) {
        return userMapper.queryUserEmail(email);
    }

    public void updatePassword(User user) {
        userMapper.updatePassword(user);
    }

    public Integer getLoginUser(HttpServletRequest request){
        String token = request.getHeader("user_token");
        Integer loginUserId = JwtUtils.getUserIdFromToken(token);
        return loginUserId;
    }
}
